# ///////////////////////////////////////////////////////////////
#
# BY: WANDERSON M.PIMENTA
# PROJECT MADE WITH: Qt Designer and PySide6
# V: 1.0.0
#
# This project can be used freely for all uses, as long as they maintain the
# respective credits only in the Python scripts, any information in the visual
# interface (G  UI) can be modified without any implication.
#
# There are limitations on Qt licenses if you want to use your products
# commercially, I recommend reading them on the official website:
# https://doc.qt.io/qtforpython/licenses.html
#
# 这个项目可以被所有用户自由使用，只要他们只在Python脚本中保留各自的原作者信息，
# 可视界面(GUI)中的任何信息都可以被修改而不需要任何暗示。
# 如果你想在商业上使用你的产品，Qt许可证是有限制的，我建议你在官方网站上阅读它们
# ///////////////////////////////////////////////////////////////

# IMPORTS
import os

# IMPORT QT CORE
from qt_core import *


class PyPushButton(QPushButton):
    def __init__(
            self,
            text="",  # 控件显示文字
            height=40,  # 控件高度设定
            minimum_width=50,  # 控件最小宽度设定
            text_padding=55,  #
            text_color="#c3ccdf",
            icon_path="",  # 控件图标
            icon_color="#c3ccdf",
            btn_color="#44475a",  # 控件浅金色
            btn_hover="#4f5368",  # 鼠标悬停(经过控件)时的颜色响应
            btn_pressed="#282a36",  # 鼠标按下控件时的颜色响应
            is_active=False  # 表示控件被按下过，选中状态显示
    ):
        super().__init__()

        # Set default parameters 修改控件默认参数
        self.setText(text)
        self.setMaximumHeight(height)
        self.setMinimumHeight(height)
        self.setCursor(Qt.PointingHandCursor)

        # Custom parameters 自定义参数设定
        self.minimum_width = minimum_width
        self.text_padding = text_padding
        self.text_color = text_color
        self.icon_path = icon_path
        self.icon_color = icon_color
        self.btn_color = btn_color
        self.btn_hover = btn_hover
        self.btn_pressed = btn_pressed
        self.is_active = is_active

        # Set style 激活设定的参数
        self.set_style(
            text_padding=self.text_padding,
            text_color=self.text_color,
            btn_color=self.btn_color,
            btn_hover=self.btn_hover,
            btn_pressed=self.btn_pressed,
            is_active=self.is_active
        )

    def set_active(self, is_active_menu):
        """ 设定控件选中时的风格，在按钮单击事件中调用"""
        self.set_style(
            text_padding=self.text_padding,
            text_color=self.text_color,
            btn_color=self.btn_color,
            btn_hover=self.btn_hover,
            btn_pressed=self.btn_pressed,
            is_active=is_active_menu
        )

    def set_style(
            self,
            text_padding=55,
            text_color="#c3ccdf",
            btn_color="#44475a",
            btn_hover="#4f5368",
            btn_pressed="#282a36",
            is_active=False
    ):
        """使用QSS语法修改QPushButton显示风格，"""
        style = f"""
        QPushButton {{
            color: {text_color};
            background-color: {btn_color};
            padding-left: {text_padding}px;
            text-align: left;
            border: none;
        }}
        QPushButton:hover {{
            background-color: {btn_hover};
        }}
        QPushButton:pressed {{
            background-color: {btn_pressed};
        }}
        """

        active_style = f"""
        QPushButton {{
            background-color: {btn_hover};
            border-right: 5px solid #282a36;
        }} 
        """  # 激活状态的标志：往内缩5个像素
        if not is_active:
            self.setStyleSheet(style)
        else:
            self.setStyleSheet(style + active_style)

    def paintEvent(self, event):
        """重写 控件绘制事件，利用此方法显示icon图标"""
        # Return default style
        QPushButton.paintEvent(self, event)

        # Painter
        qp = QPainter()
        qp.begin(self)
        qp.setRenderHint(QPainter.Antialiasing)
        qp.setPen(Qt.NoPen)

        rect = QRect(0, 0, self.minimum_width, self.height())

        self.draw_icon(qp, self.icon_path, rect, self.icon_color)

        qp.end()

    def draw_icon(self, qp, image, rect, color):
        """从指定路径下，找到控件图标"""
        # Format Path
        app_path = os.path.abspath(os.getcwd())
        folder = "gui/images/icons"
        path = os.path.join(app_path, folder)
        icon_path = os.path.normpath(os.path.join(path, image))

        # Draw icon
        icon = QPixmap(icon_path)
        painter = QPainter(icon)
        painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
        painter.fillRect(icon.rect(), color)
        qp.drawPixmap(
            (rect.width() - icon.width()) / 2,
            (rect.height() - icon.height()) / 2,
            icon
        )
        painter.end()
